package com.lanzhou.yuanfen.security;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
 * @ClassName: {@link SocialDetails}
 * @Description: 社交账户详情, 只存储唯一表示SocialKey[仅限于第一次社交登入的情况]
 * @author: Lanzhou
 * @date: 2020/5/31 9:28
 * @QQ: 1627518680
 * @Copyright:2020 All rights reserved.
 */
public class SocialDetails implements UserDetails {

    private String socialKey;

    private List<SimpleGrantedAuthority> authorities;

    public SocialDetails() {
    }

    public SocialDetails(String socialKey, List<SimpleGrantedAuthority> authorities) {
        this.socialKey = socialKey;
        this.authorities = authorities;
    }

    public String getSocialKey() {
        return socialKey;
    }

    public void setSocialKey(String socialKey) {
        this.socialKey = socialKey;
    }

    public void setAuthorities(List<SimpleGrantedAuthority> authorities) {
        this.authorities = authorities;
    }

    /**
     * 判断是否是同一个用户用的是Equals
     *
     * @param o
     * @return
     */
    @Override
    public boolean equals(Object o) {
        return o instanceof SocialDetails && this.getUsername().equals(((SocialDetails) o).getSocialKey());
    }

    /**
     * 判断是否是同一个用户用的是hashCode
     * @return
     */
    @Override
    public int hashCode() {
        return Objects.hash(socialKey);
    }

    @Override
    public String toString() {
        return "SocialDetails{" +
                "socialKey='" + socialKey + '\'' +
                '}';
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    @Override
    public String getPassword() {
        return null;
    }

    @Override
    public String getUsername() {
        return this.socialKey;
    }

    /**
     * 账户未过期
     *
     * @return
     */
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    /**
     * 账户未锁定
     *
     * @return
     */
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    /**
     * 账户凭证未过期
     *
     * @return
     */
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /**
     * 账户是否可用
     *
     * @return
     */
    @Override
    public boolean isEnabled() {
        return true;
    }

    private boolean logout = true;

    public boolean isLogout() {
        return logout;
    }

    public void setLogout(boolean logout) {
        this.logout = logout;
    }

}
